#include "newsqlflayerdialog.h"
#include "ui_newsqlflayerdialog.h"

NewSQLFLayerDialog::NewSQLFLayerDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::NewSQLFLayerDialog)
{
    ui->setupUi(this);
    iniMainDialog();
    connect(this,&NewSQLFLayerDialog::sigAddNewSQLFLayer,
            AppEvent::getInstance(),&AppEvent::on_AddNewSQLFLayer);
}

NewSQLFLayerDialog::~NewSQLFLayerDialog()
{
    delete ui;
}

void NewSQLFLayerDialog::iniMainDialog()
{

    comboFlayerType=new QComboBox();
    comboFlayerType->addItem(QObject::tr("Point"));
    comboFlayerType->addItem(QObject::tr("LineString"));
    comboFlayerType->addItem(QObject::tr("Polygon"));

    editFlayerName=new QLineEdit();
    QFrame *frame=new QFrame();
    QFormLayout *formLayout=new QFormLayout(frame);
    formLayout->setSpacing(6);
    formLayout->setContentsMargins(11,11,11,11);
    formLayout->insertRow(0,QObject::tr("LayerName:"),comboFlayerType);
    formLayout->insertRow(0,QObject::tr("LayerType:"),editFlayerName);

    btnOK=new QPushButton();
    btnOK->setText(QObject::tr("OK"));
    connect(btnOK,&QPushButton::clicked,
            this,&NewSQLFLayerDialog::on_btnOK_clicked);

    btnCancel=new QPushButton();
    btnCancel->setText(QObject::tr("Cancel"));
    connect(btnCancel,&QPushButton::clicked,
            this,&NewSQLFLayerDialog::on_btnCancel_clicked);

    QVBoxLayout *mainLayout=new QVBoxLayout();
    QHBoxLayout *btnLayout=new QHBoxLayout();
    btnLayout->addWidget(btnOK);
    btnLayout->addWidget(btnCancel);
    mainLayout->addWidget(frame);
    mainLayout->addLayout(btnLayout);
    this->setLayout(mainLayout);
}

void NewSQLFLayerDialog::on_btnOK_clicked()
{
    QString layerName=editFlayerName->text();
    QString layerType=comboFlayerType->currentText();
    if(!layerName.isEmpty()&&layerType=="Point")
    {
        GeometryType geomType=GeometryType::kPoint;
        emit sigAddNewSQLFLayer(layerName,geomType);
    }
    if(!layerName.isEmpty()&&layerType=="LineString")
    {
        GeometryType geomType=GeometryType::kLineString;
        emit sigAddNewSQLFLayer(layerName,geomType);
    }
    if(!layerName.isEmpty()&&layerType=="Polygon")
    {
        GeometryType geomType=GeometryType::kLineString;
        emit sigAddNewSQLFLayer(layerName,geomType);
    }
    this->close();
}

void NewSQLFLayerDialog::on_btnCancel_clicked()
{
    this->close();
}
